package com.blb.ssh.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.blb.ssh.entity.Menu;
import com.blb.ssh.entity.Role;
import com.blb.ssh.service.RoleService;

public class RoleAction {
	private Role role;
	private RoleService roleService;
	private String ids;
	private int page,rows;
	private int roleId;
	private String roleName;
	private List<Menu> mList=new ArrayList<Menu>();
	
	private List<Map<String, Object>> powerData;
	private Map<String,Object> resultMap = new HashMap<String,Object>();
	
	
	
	//查询全部
	public String selectRolePageData(){
		List<Role> roleList;
		Long roleCount=roleService.getRoleCount();
		if(StringUtils.isNotEmpty(roleName)){
			roleList=roleService.getRoleListByMap(roleName,page,rows);
		}else{
			roleList=roleService.getRoleListByMap(page,rows);
		}
		resultMap.put("total", roleCount);
		resultMap.put("rows", roleList);
		return "data";
	}
	
	//用户删除
	public String batchDeleteRole(){
		resultMap.put("isok", roleService.batchDeleteRole(ids));
		return "data";
	}
	
	//新增用户
	public String addRole(){
		resultMap.put("isok", roleService.addRole(role));
		return "data";
	}

	//修改用户
	public String editRole(){
		resultMap.put("isok", roleService.editRole(role));
		return "data";
	}
	//获取roleid对应的全部menu信息
	public String getmenuId(){
		mList=roleService.getmenuId(roleId);
		return "data2";
	}

	/**
	 * 获取完整的树
	 * @return
	 */
	public String getAllTree(){
		//获取该角色的所有菜单选项
		List<Menu> MenuList=roleService.getAllTree();
		//通过循环来获取第一层的节点
		List<Menu> PowerLv1MenuList=new ArrayList<Menu>();
		for(Menu Lv1menu:MenuList){
			int pid=Lv1menu.getMenuPid();
			if(pid==0){
				PowerLv1MenuList.add(Lv1menu);
			}
		}
		
		for(Menu menu:PowerLv1MenuList){
			Map<String,Object> PowerLv1MapList=new HashMap<String,Object>();
			PowerLv1MapList.put("id", menu.getMenuId());
			PowerLv1MapList.put("text", menu.getMenuName());
			PowerLv1MapList.put("pid", menu.getMenuPid());
			PowerLv1MapList.put("url", menu.getMenuUrl());
			
			getChildNodesByRole(menu.getMenuId(),PowerLv1MapList,MenuList);
			powerData=new ArrayList<Map<String, Object>>();
			powerData.add(PowerLv1MapList);
		}
		return "data3";
	}
	
	/**
	 * currentHeroId :当前节点的父ID
	 * parentMap:上一级map数据对象
	 * childList:下一级节点的所有数据
	 */
	private void getChildNodesByRole(int menuId, Map<String, Object> powerLv1MapList,List<Menu> menuList) {
		//通过循环来获取下一层的节点
		List<Menu> nextMenuList=new ArrayList<Menu>();
		for(Menu nextmenu:menuList){
			int pid=nextmenu.getMenuPid();
			if(menuId==pid){
				nextMenuList.add(nextmenu);
			}
		}
		
		if(nextMenuList.size()>0){
			//创建一个List<Map<String, Object>>用来保存这一层的节点信息
			List<Map<String, Object>> nextLvList = new ArrayList<Map<String, Object>>();
			for(Menu lvmenu:nextMenuList){
				Map<String,Object> NextMapList=new HashMap<String,Object>();
				NextMapList.put("id", lvmenu.getMenuId());
				NextMapList.put("text", lvmenu.getMenuName());
				NextMapList.put("pid", lvmenu.getMenuPid());
				NextMapList.put("url", lvmenu.getMenuUrl());
				
				getChildNodesByRole(lvmenu.getMenuId(),NextMapList,menuList);
				
				nextLvList.add(NextMapList);
			}
			powerLv1MapList.put("children", nextLvList);
			powerLv1MapList.put("state", "close");
		}else{
			powerLv1MapList.put("state", "open");
		}
	}
	
	//权限修改提交
	public String submitTree(){
		resultMap.put("isok", roleService.editRole(ids,roleId));
		return "data";
	}
	
	
	
	
	
	
	
	public List<Map<String, Object>> getPowerData() {
		return powerData;
	}

	public void setPowerData(List<Map<String, Object>> powerData) {
		this.powerData = powerData;
	}

	public List<Menu> getmList() {
		return mList;
	}

	public void setmList(List<Menu> mList) {
		this.mList = mList;
	}

	public int getRoleId() {
		return roleId;
	}

	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public Role getRole() {
		return role;
	}
	public void setRole(Role role) {
		this.role = role;
	}
	public RoleService getRoleService() {
		return roleService;
	}
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public Map<String, Object> getResultMap() {
		return resultMap;
	}
	public void setResultMap(Map<String, Object> resultMap) {
		this.resultMap = resultMap;
	}
}
